package jh.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.jh.ios.forecast.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import jh.fragment.FragmentOpenNumber;
import jh.model.MTypeModel;
import jh.utils.HttpUtils;
import jh.view.CustomViewPager;

public class TabPageActivity extends BaseActivity {

    private TabLayout tabLayout;
    private CustomViewPager viewPager;
    private List<MTypeModel> datas = new ArrayList<>();
    private List<FragmentOpenNumber> fragments = new ArrayList<>();
    private MyPagerAdapter pagerAdapter;
    @Override
    public int getLayoutId() {
        return R.layout.activity_tab_page;
    }

    @Override
    public void initData(Bundle savedInstanceState) {



        getTagPage();
    }





    @Override
    public void initView() {

        backButton = find(R.id.back_buton);
        showBackButton();
        title = find(R.id.navigation_title);
        title.setText("彩票开奖");

        tabLayout = find(R.id.tabBar);
        viewPager = find(R.id.viewPager);
        FragmentManager fragmentManager = getSupportFragmentManager();
        pagerAdapter = new MyPagerAdapter(fragmentManager);

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(fragments.size()-1);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void getTagPage(){

        String url = "/api/lotteryCategory/getParentLotteryCategory";

        HttpUtils.POST(url, new HashMap<String, String>(), new HttpUtils.HttpResponse() {
            @Override
            public void callback(JSONObject object, String msg, Exception e) {

                if (e == null){
                    if (msg == null){

                        try {
                            JSONArray jsonArray = object.getJSONArray("data");

                            GsonBuilder builder = new GsonBuilder();
                            Gson gson = builder.create();
                            List<MTypeModel> list = gson.fromJson(jsonArray.toString(),new TypeToken<List<MTypeModel>>(){}.getType());

                            MTypeModel model = new MTypeModel();
                            model.setParentLotteryCategoryCode("");
                            model.setParentLotteryCategoryName("全部彩钟");
                            datas.add(model);
                            datas.addAll(list);
                            for(int i=0;i<datas.size();i++){
                                FragmentOpenNumber fragmentOpenNumber = new FragmentOpenNumber();
                                Bundle bundle = new Bundle();
                                bundle.putString("code",datas.get(i).getParentLotteryCategoryCode());
                                fragmentOpenNumber.setArguments(bundle);
                                fragments.add(fragmentOpenNumber);
                            }
                            pagerAdapter.notifyDataSetChanged();

                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }


                    }
                }

            }
        });


    }


    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return datas.get(position).getParentLotteryCategoryName();
        }
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }
        @Override
        public int getCount() {
            return fragments.size();
        }
    }


}
